﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Net;
using System.Net.Mail;
using CS2102_Music.Logic;

namespace CS2102_Music.Logic
{
    public partial class Logic
    {
        public bool AutomaticEmailInvoice(LogicModels.UserL customer, Cart.TransactionCart transaction)
        {
            bool send = true;
            string transactionSummary = "";
            string email = customer.Email;

            transactionSummary = transactionSummary + "\n\nSongs:\n";
            for (int i = 0; i < transaction.Songs.Count; i++)
            {
                transactionSummary = transactionSummary + "Product: " + transaction.Songs[i].Title + "\n";
                transactionSummary = transactionSummary + "ID: " + transaction.Songs[i].ID + "\n";
            }

            transactionSummary = transactionSummary + "\n\nAlbums:\n";
            for (int i = 0; i < transaction.Albums.Count; i++)
            {
                transactionSummary = transactionSummary + "Product: " + transaction.Albums[i].Title + "\n";
                transactionSummary = transactionSummary + "ID: " + transaction.Albums[i].ID + "\n"; ;
            }
            transactionSummary = transactionSummary + "\n\n\n\n" + "Total Cost: $ " + transaction.TotalCost;

            DateTime date = DateTime.Now;

            NetworkCredential cred = new NetworkCredential("cs2102G15MusicStore@gmail.com", "cs2102G15MusicStore@@@");
            MailMessage msg = new MailMessage();
            msg.To.Add(email);
            msg.From = new MailAddress("cs2102G15MusicStore@gmail.com");
            msg.Subject = "G15 CS2102 Music Store - Invoice for your purchase on: " + date;
            msg.Body =
                "Dear customer, \n" + "These are your purchases: \n\n" + transactionSummary + "\n\n\n\n" +  "Thank you for shopping with us!";

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.Credentials = cred; // Send our account login details to the client.
            client.EnableSsl = true;   // Read below.
            client.Send(msg);          // Send our email.

            return send;
        }


    }
}